package top.jamsee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.jamsee.entity.Buy_data;
import top.jamsee.entity.Lent_data;
import top.jamsee.entity.ResultInfo;
import top.jamsee.service.LentService;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping(value = "/lent")
@Controller
public class LentController extends BaseController{



        @Resource
        LentService lentService;
        int allPages=0; //所有页数


        //跳转到买房页面的接口
        @RequestMapping(value = "/buyBlank.do",method = RequestMethod.GET)
        public ModelAndView buy_blank(){
            ModelAndView mv=new ModelAndView();
            mv.setViewName("lent");
            return mv;
        }
        //跳转到租房页面的接口
        @RequestMapping(value = "/lentBlank.do")
        public ModelAndView lent_blank(){
            ModelAndView mv=new ModelAndView();
            mv.setViewName("forward:/WEB-INF/view/lent.html");
            return mv;
        }
        //跳转到买房租房登记页面的接口
        @RequestMapping(value = "/buyLentRegBlank.do")
        public ModelAndView buyLentReg_blank(){
            ModelAndView mv=new ModelAndView();
            mv.setViewName("buyAndLentRegister");
            return mv;
        }



        /*
         * 处理租房的数据响应ajax
         *
         * */
        @RequestMapping(value = "/lentPage.do")
        @ResponseBody
        public List<Lent_data> lentPage(HttpServletRequest req,HttpServletResponse resp,int pageNum){
            //如果用户没有登录则不给他数据
            if (session.getAttribute("user")==null){
                return new ArrayList<Lent_data>();
            }
            //获取一个 ls对象,里面包括 数据信息和分页的总数
            List<List> ls=lentService.SelectLentData(pageNum);
            List<Lent_data> lent_data=ls.get(0);
            List pageList=ls.get(1);
            //总页数
            allPages = (Integer) pageList.get(0);
            return lent_data;




        }
        //返回总的页数
        @RequestMapping(value = "/allPage.do")
        @ResponseBody
        public int allPage(HttpServletRequest req,HttpServletResponse resp){
            return lentService.selectLentNum();
        }

        //根据编号删除数据
        @RequestMapping(value = "/lentDel.do",method = RequestMethod.POST)
        @ResponseBody
        public int lentDel(HttpServletRequest req,HttpServletResponse resp,int id){
            //如果用户没有登录则不给他数据
            if (session.getAttribute("user")==null) {
                //没有权限删除数据
                return 0;
            }
            int res = lentService.deleteLentData(id);
            return res;





        }
        //添加租房表的记录
        @RequestMapping(value = "/addLent.do")
        @ResponseBody
        public ResultInfo addLent(Lent_data lentData, HttpServletRequest request){
            //在作用域中得到用户
            String user="user";
            if (session.getAttribute("user")==null) {
                //没有权限添加
                return new ResultInfo(false,"请登录用户");
            }
            //获取当前系统时间
           lentData.setInsert_time(new Date());
            // new Date()为获取当前系统时间
            ResultInfo res=lentService.addLentData(lentData,request);

            return res;



        }
        /*
         * 模糊查询lent表
         * @part :传入部分的字符串
         * @pageNum: 传入的页数
         *
         *
         * */
        @RequestMapping(value = "/partLent.do")
        @ResponseBody
        public List<Lent_data> partLent(String part,int pageNum){
            //没有权限模糊查询
            //在作用域中得到用户
            String user="user";
            if (session.getAttribute("user")==null) {
                return new ArrayList<Lent_data>();
            }

            //获取一个 ls对象,里面包括 数据信息和分页的总数
            List ls = lentService.selectLentByData(pageNum, part);
            List<Lent_data> lent_data = (List<Lent_data>) ls.get(0);
            //总页数
            allPages = (Integer) ls.get(1);
            return lent_data;

        }
        //返回总的页数(当模糊查询时)
        @RequestMapping(value = "/allPageSelectWhenSearch.do")
        @ResponseBody
        public int allPageSelectWhenSearch(String value){

            return lentService.selectLentByDataNum(value);
        }
        //返回总的页数(当查询时)
        @RequestMapping(value = "/allPageSelect.do")
        @ResponseBody
        public int allPageSelect(){
            return allPages;
        }
        //返回总的页数(当查询时)
        @RequestMapping(value = "/updateLent.do",method = RequestMethod.POST)
        @ResponseBody
        public ResultInfo updateLent(int updateId,Lent_data lent_data,HttpServletRequest request){
            //更新插入时间
            lent_data.setInsert_time(new Date());
            lent_data.setId(updateId);
            if (session.getAttribute("user")==null) {
                return new ResultInfo(false,"没有权限更新数据");
            }

            //搜索不到该记录S
            if (lentService.selectLentById(updateId)==0){
                return new ResultInfo(false,"搜索不到该记录");
            }

            ResultInfo upDateRes=lentService.upDateLentById(lent_data,request);

            return upDateRes;
        }











}
